<template>
  <div class="login_box">
    <div class="login_com">
      <h3>新冠物资管理平台</h3>
      <el-form
        status-icon
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
      >
        <el-form-item prop="username">
          <el-input
            @keyup.enter.native="handleSubmit"
            type="text"
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            @keyup.enter.native="handleSubmit"
            prefix-icon="el-icon-goods"
            type="password"
            v-model="loginForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-checkbox v-model="checked" class="rememberme">记住密码</el-checkbox>
        <el-form-item style="float: right">
          <el-button
            size="small"
            type="primary"
            @click="handleSumbit"
            icon="el-icon-mobile"
            >登录</el-button
          >
          <el-button size="small" @click="handleReset" icon="el-icon-refresh"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <Vcode
      :show="show"
      @success="onLogin"
      @close="close"
      :canvasWidth="500"
      :canvasHeight="350"
    ></Vcode>
  </div>
</template>

<script>
import Vcode from 'vue-puzzle-vcode'
import { loginApi, getUserInfoApi } from '../../api/index'
export default {
  name: 'Login',
  components: { Vcode },
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      checked: false,
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 12,
            message: '长度在 3 到 12 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      },
      show: false
    }
  },
  methods: {
    // 用户点击了重置
    handleReset () {
      this.$refs.loginForm.resetFields()
      this.loginForm.username = ''
      this.loginForm.password = ''
    },
    // 用户点击了登录
    handleSumbit () {
      this.$refs.loginForm.validate((valid) => {
        if (!valid) return
        this.show = true
      })
    },
    // 登录的具体事件
    async onLogin () {
      const res = await loginApi(this.loginForm)
      if (!res.success) return this.$message.error(res.data.errorMsg)
      this.$message.success('登录成功')
      if (this.checked) {
        localStorage.setItem('login', JSON.stringify(this.loginForm))
      }
      window.localStorage.setItem('token', res.data)
      this.getUserInfo()
      // 跳转路由
      this.$router.push('/home')
    },
    async getUserInfo () {
      const res = await getUserInfoApi()
      if (!res.success) return this.$message.error(res.data.errorMsg)
      window.localStorage.setItem('user', JSON.stringify(res.data))
    },
    // 关闭图片验证
    close () {
      this.show = false
    }
  },
  created () {
    // 保存密码功能 正常来说这个业务应该是保存到cookie的
    const login = JSON.parse(localStorage.getItem('login'))
    if (login) {
      this.loginForm = login
    }
  }
}
</script>

<style lang='less' scoped>
.login_box {
  height: 100%;
  background-color: #2d3a4b;
  position: relative;
  .login_com {
    width: 400px;
    height: 300px;
    border: 1px solid rgba(0, 0, 0, 0.5);
    border-radius: 10px;
    position: absolute;
    background-color: #fff;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    h3 {
      height: 60px;
      font-size: 22px;
      font-weight: 700;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .el-form {
      margin: 35px 35px 0px 35px;
    }
  }
}
</style>
